package com.hsz.motion.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.hsz.motion.R;
import com.hsz.motion.commmon.bean.SportMotionRecord;
import com.hsz.motion.commmon.utils.LogUtils;
import com.hsz.motion.commmon.utils.MySp;
import com.hsz.motion.databinding.ActivitySportsBinding;
import com.hsz.motion.db.DataManager;
import com.hsz.motion.db.RealmHelper;
import com.hsz.motion.ui.BaseActivity;
import com.hsz.motion.ui.permission.PermissionHelper;
import com.hsz.motion.ui.permission.PermissionListener;
import com.hsz.motion.ui.permission.Permissions;

import java.text.DecimalFormat;
import java.util.List;
/**
 * 运动轨迹
 */
public class SportsActivity extends BaseActivity {
    private ActivitySportsBinding mBinding;
    private DecimalFormat decimalFormat = new DecimalFormat("0.00");

    private final int SPORT = 0x0012;

    private DataManager dataManager = null;
    @Override
    public int getLayoutId() {
        return R.layout.activity_sports;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mBinding= (ActivitySportsBinding) mBaseBinding;
        dataManager = new DataManager(new RealmHelper());

        upDateUI();
    }

    private void upDateUI() {
        try {
            List<SportMotionRecord> records = dataManager.queryRecordList(Integer.parseInt(SPUtils.getInstance().getString(MySp.USERID, "0")));
            if (null != records) {

                double sportMile = 0;
                long sportTime = 0;
                for (SportMotionRecord record : records) {
                    sportMile += record.getDistance();
                    sportTime += record.getDuration();
                }
                mBinding.tvSportMile.setText(decimalFormat.format(sportMile / 1000d));
                mBinding.tvSportCount.setText(String.valueOf(records.size()));
                mBinding.tvSportTime.setText(decimalFormat.format((double) sportTime / 60d));
            }
        } catch (Exception e) {
            LogUtils.e("获取运动数据失败", e);
        }
    }


    @Override
    public void initListener() {
        mBinding.btStart.setOnClickListener(this);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK)
            return;
        switch (requestCode) {
            case SPORT:
                upDateUI();
                setResult(RESULT_OK);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        if (null != dataManager)
            dataManager.closeRealm();
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.btStart:
                PermissionHelper.requestPermissions(this, Permissions.PERMISSIONS_LOCATION,
                        getResources().getString(R.string.app_name) + "需要获取位置", new PermissionListener() {
                            @Override
                            public void onPassed() {
                                startActivityForResult(new Intent(SportsActivity.this, SportMapActivity.class), SPORT);
                            }
                        });
                break;
            default:
                break;
        }

    }
}
